home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Jumpstart / Multimedia Microsoft Jumpstart Version 1.1a (Microsoft).BIN / develpmt / source / dialogs / dialogs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-02  |  13.3 KB  |  483 lines

  1. /*
  2.  * DIALOGS.C
  3.  *
  4.  * Main entry code for application to use each one of the Common Dialogs
  5.  * in the Windows 3.1 COMMDLG.DLL.  Code in this file simply calls code
  6.  * in one of the other files that deals specifically with a single
  7.  * common dialog.
  8.  *
  9.  * Copyright (c)1992 Microsoft Corporation, All Right Reserved
  10.  *
  11.  * Kraig Brockschmidt, Software Design Engineer
  12.  * Microsoft Systems Developer Relations
  13.  * One Microsoft Way
  14.  * Redmond, WA  98052
  15.  *
  16.  * Internet  :  kraigb@microsoft.com
  17.  * Compuserve:  70750,2344
  18.  * Fax       :  (206)936-7329
  19.  */
  20.  
  21.  
  22. //We need commdlg.h for registered message strings.
  23. #include <windows.h>
  24. #include <commdlg.h>
  25. #include "dialogs.h"
  26.  
  27.  
  28.  
  29. HWND        hgWnd;          //Main window
  30. HANDLE      hgInst;         //Application instance needed to load resources
  31. HANDLE      hBrush=NULL;    //Main window class background brush
  32.  
  33.  
  34. //Specific registered messages that the Common Dialogs might send
  35. UINT        iMSGHelp;
  36. UINT        iMSGFind;
  37. UINT        iMSGShareViolation;
  38. UINT        iMSGFileOK;
  39. UINT        iMSGListboxChange;
  40. UINT        iMSGColorOK;
  41. UINT        iMSGSetRGB;
  42.  
  43.  
  44.  
  45.  
  46.  
  47. /*
  48.  * WinMain
  49.  *
  50.  * Purpose:
  51.  *  Main entry point of application.   Should register the app class
  52.  *  if a previous instance has not done so and do any other one-time
  53.  *  initializations.
  54.  *
  55.  * Parameters:
  56.  *  Standard
  57.  *
  58.  * Return Value:
  59.  *  Value to return to Windows--termination code.
  60.  */
  61.  
  62. int PASCAL WinMain (HANDLE hInst, HANDLE hPrevInst, LPSTR pszCmd, int nCmdShow)
  63.     {
  64.     WNDCLASS        wc;
  65.     HWND            hWnd;
  66.     MSG             msg;
  67.  
  68.     hgInst=hInst;
  69.     hBrush=CreateSolidBrush(GetSysColor(COLOR_APPWORKSPACE));
  70.  
  71.     if (!hPrevInst)
  72.         {
  73.         //Main window class
  74.         wc.style          = CS_HREDRAW | CS_VREDRAW;
  75.         wc.lpfnWndProc    = DialogsWndProc;
  76.         wc.cbClsExtra     = 0;
  77.         wc.cbWndExtra     = 0;
  78.         wc.hInstance      = hInst;
  79.         wc.hIcon          = LoadIcon(hInst, "Icon");
  80.         wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  81.         wc.hbrBackground  = hBrush;
  82.         wc.lpszMenuName   = MAKEINTRESOURCE(IDR_MENU);
  83.         wc.lpszClassName  = "Dialogs";
  84.  
  85.         if (!RegisterClass(&wc))
  86.             return FALSE;
  87.  
  88.         //Hidden popup class for creating modeless dialogs.
  89.         wc.lpfnWndProc    = PopupWndProc;
  90.         wc.hIcon          = NULL;
  91.         wc.hbrBackground  = COLOR_WINDOW+1;
  92.         wc.lpszMenuName   = NULL;
  93.         wc.lpszClassName  = "ModelessPopup";
  94.  
  95.         if (!RegisterClass(&wc))
  96.             return FALSE;
  97.         }
  98.  
  99.  
  100.     /*
  101.      * Register common dialog specific messages using strings defined in
  102.      * COMMDLG.H.  Only the Help and Find messages are sent to our main
  103.      * window procedure (the owner of the dialog box requesting help).
  104.      * All others are sent to a dialog's hook procedure if one exists.
  105.      */
  106.     iMSGHelp          =RegisterWindowMessage(HELPMSGSTRING);  //All Dialogs
  107.     iMSGFind          =RegisterWindowMessage(FINDMSGSTRING);  //Find & Replace
  108.     iMSGShareViolation=RegisterWindowMessage(SHAREVISTRING);  //Open & Save
  109.     iMSGFileOK        =RegisterWindowMessage(FILEOKSTRING);   //Open & Save
  110.     iMSGListboxChange =RegisterWindowMessage(LBSELCHSTRING);  //Open & Save
  111.     iMSGColorOK       =RegisterWindowMessage(COLOROKSTRING);  //Color
  112.     iMSGSetRGB        =RegisterWindowMessage(SETRGBSTRING);   //Color
  113.  
  114.  
  115.     hWnd=CreateWindow("Dialogs", "Common Dialogs"
  116.                       , WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW
  117.                       , 35, 35, 400, 250
  118.                       , NULL, NULL, hInst, NULL);
  119.  
  120.     hgWnd=hWnd;
  121.     ShowWindow(hWnd, nCmdShow);
  122.     UpdateWindow(hWnd);
  123.  
  124.     while (GetMessage(&msg, NULL, 0,0 ))
  125.         {
  126.         /*
  127.          * Find and replace are modeless dialogs requiring us to call
  128.          * IsDialogMessage for each.  We use the dialog handles here to
  129.          * determine if that dialog is active or not.  The handles are
  130.          * set in functions in search.c.
  131.          */
  132.         if (NULL!=hDlgFind && IsDialogMessage(hDlgFind, &msg))
  133.             continue;
  134.  
  135.         if (NULL!=hDlgReplace && IsDialogMessage(hDlgReplace, &msg))
  136.             continue;
  137.  
  138.         TranslateMessage(&msg);
  139.         DispatchMessage(&msg);
  140.         }
  141.  
  142.     if (NULL!=hgFont)
  143.         DeleteObject(hgFont);
  144.  
  145.     DeleteObject(hBrush);
  146.     return msg.wParam;
  147.     }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. /*
  155.  * DialogsWndProc
  156.  *
  157.  * Purpose:
  158.  *  Window class procedure.  Standard callback.
  159.  *
  160.  * Parameters:
  161.  *  Standard.
  162.  *
  163.  * Return Value:
  164.  *  Standard.
  165.  */
  166.  
  167. LONG FAR PASCAL DialogsWndProc(HWND hWnd, UINT iMsg, UINT wParam, LONG lParam)
  168.     {
  169.     COLORREF        cr;
  170.     HBRUSH          hBrT;
  171.     HFONT           hFontT;
  172.     PAINTSTRUCT     ps;
  173.     FARPROC         pfn;
  174.  
  175.     WORD            wID;
  176.  
  177.     /*
  178.      * We receive this message whenever help is enabled in any dialog,
  179.      * regardless of whether or not you have a hook installed.  Therefore
  180.      * you have to be careful that this message processing and the hook
  181.      * don't BOTH attempt to provide help, such as calling WinHelp.
  182.      */
  183.     if (iMSGHelp==iMsg)
  184.         {
  185.         MessageBeep(0);
  186.         return 0L;
  187.         }
  188.  
  189.  
  190.     /*
  191.      * If we get the Find/Replace message, pass it to a handler doing
  192.      * the appropriate typecast of lParam to a pointer to a FINDREPLACE.
  193.      */
  194.     if (iMSGFind==iMsg)
  195.         return LSearchMessageHandler(hWnd, (LPFINDREPLACE)lParam);
  196.  
  197.  
  198.     switch (iMsg)
  199.         {
  200.         case WM_PAINT:
  201.             /*
  202.              * Draw some text using the last font & color selected in
  203.              * a ChooseFont dialog.  The background is automatically
  204.              * painted with the window class background brush that is
  205.              * changed in the ColorDialogs case below.
  206.              */
  207.             BeginPaint(hWnd, &ps);
  208.             SetTextColor(ps.hdc, crgFont);      //crgFont in COLOR.C
  209.             SetBkMode(ps.hdc, TRANSPARENT);
  210.  
  211.             if (NULL!=hgFont)
  212.                 hFontT=SelectObject(ps.hdc, hgFont);
  213.  
  214.             TextOut(ps.hdc, 10, 10, "Change This Font...", 19);
  215.  
  216.             if (NULL!=hgFont)
  217.                 SelectObject(ps.hdc, hFontT);
  218.  
  219.             EndPaint(hWnd, &ps);
  220.             break;
  221.  
  222.  
  223.         case WM_DESTROY:
  224.             PostQuitMessage(0);
  225.             break;
  226.  
  227.  
  228.         case USER_CHANGECOLOR:
  229.             /*
  230.              * We send this message to ourselves with an RGB value in
  231.              * lParam to change the background window color.  We use
  232.              * it both from the WM_COMMAND case for ChooseColor below
  233.              * and from the ColorModelessHook in COLOR.C.
  234.              *
  235.              * Replace the current background brush with one of the
  236.              * newly selected color.
  237.              */
  238.             hBrush=CreateSolidBrush((COLORREF)lParam);
  239.             hBrT=SetClassWord(hWnd, GCW_HBRBACKGROUND, (WORD)hBrush);
  240.             DeleteObject(hBrT);
  241.  
  242.             InvalidateRect(hWnd, NULL, TRUE);
  243.             UpdateWindow(hWnd);
  244.             break;
  245.  
  246.  
  247.  
  248.         case WM_COMMAND:
  249.             /*
  250.              * In Win32 wParam is packed with the ID and the notification
  251.              * unlike Win16 where a notification is always in HIWORD(lParam).
  252.              * LOWORD(wParam) gives us the command ID in either case.
  253.              */
  254.             wID=LOWORD(wParam);
  255.  
  256.             /*
  257.              * Since we have ranges of commands to dispatch to the various
  258.              * functions in other modules, checking ranges with if statements
  259.              * is a little more convenient than a bunch of switch/case
  260.              * statements.
  261.              */
  262.             if (wID >=IDM_COLORMIN && wID <=IDM_COLORMAX)
  263.                 {
  264.                 cr=ColorDialogs(hWnd, wID);
  265.  
  266.                 if (0xFFFFFFFF!=cr)
  267.                     SendMessage(hWnd, USER_CHANGECOLOR, 0, (LONG)cr);
  268.  
  269.                 break;
  270.                 }
  271.  
  272.  
  273.             if (wID >=IDM_FONTMIN && wID <=IDM_FONTMAX)
  274.                 {
  275.                 if (FontDialogs(hWnd, wID, dwFlags))
  276.                     {
  277.                     InvalidateRect(hWnd, NULL, TRUE);
  278.                     UpdateWindow(hWnd);
  279.                     }
  280.                 break;
  281.                 }
  282.  
  283.  
  284.             if (wID >=IDM_FILEMIN && wID <=IDM_FILEMAX)
  285.                 {
  286.                 FileDialogs(hWnd, wID);
  287.                 break;
  288.                 }
  289.  
  290.  
  291.             if (wID >=IDM_PRINTMIN && wID <=IDM_PRINTMAX)
  292.                 {
  293.                 PrintDialogs(hWnd, wID);
  294.                 break;
  295.                 }
  296.  
  297.  
  298.             if (wID >=IDM_SEARCHMIN && wID <=IDM_SEARCHMAX)
  299.                 {
  300.                 SearchDialogs(hWnd, wID);
  301.                 break;
  302.                 }
  303.  
  304.  
  305.             switch (wID)
  306.                 {
  307.                 case IDM_EXIT:
  308.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  309.                     break;
  310.  
  311.                 case IDM_FONTSELECTFLAGS:
  312.                     /*
  313.                      * Display a dialog in which the user specifies which
  314.                      * flags to use in the next call to ChooseFont.  The
  315.                      * dialog procedure SelectFontFlagsProc below maniuplates
  316.                      * the global varaible dwFlags (defined in FONT.C).
  317.                      */
  318.                     pfn=MakeProcInstance(SelectFontFlagsProc, hgInst);
  319.                     DialogBox(hgInst, MAKEINTRESOURCE(IDD_SELECTFONTFLAGS), hWnd, pfn);
  320.                     FreeProcInstance(pfn);
  321.                     break;
  322.  
  323.                 case IDM_HELPABOUT:
  324.                     pfn=MakeProcInstance(AboutProc, hgInst);
  325.                     DialogBox(hgInst, MAKEINTRESOURCE(IDD_ABOUT), hWnd, pfn);
  326.                     FreeProcInstance(pfn);
  327.                     break;
  328.                 }
  329.  
  330.         default:
  331.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  332.         }
  333.  
  334.     return 0L;
  335.     }
  336.  
  337.  
  338.  
  339.  
  340.  
  341. /*
  342.  * SelectFontFlagsProc
  343.  *
  344.  * Purpose:
  345.  *  Handles the dialog to select various flags that affect which fonts
  346.  *  are shown in the ChooseFont dialog.  The results of this dialog
  347.  *  are combined into the dwFlags global variable.
  348.  *
  349.  * Parameters:
  350.  *  Standard
  351.  *
  352.  * Return Value:
  353.  *  Standard
  354.  */
  355.  
  356. BOOL FAR PASCAL SelectFontFlagsProc(HWND hDlg, UINT iMsg, UINT wParam, LONG lParam)
  357.     {
  358.     UINT            i;
  359.  
  360.     //Mapping from control ID-3 to ChooseFont flag.
  361.     static DWORD    mpIDFlag[7]={CF_ANSIONLY, CF_FIXEDPITCHONLY,
  362.                                  CF_NOVECTORFONTS, CF_NOSIMULATIONS,
  363.                                  CF_SCALABLEONLY, CF_TTONLY, CF_WYSIWYG};
  364.  
  365.  
  366.     switch (iMsg)
  367.         {
  368.         case WM_INITDIALOG:
  369.             //Set the appropriate Printer, Screen, or Both radiobutton
  370.             if (CF_BOTH==(dwFlags & CF_BOTH))
  371.                 i=ID_BOTH;
  372.             else
  373.                 i=(dwFlags & CF_PRINTERFONTS) ? ID_PRINTERFONTS : ID_SCREENFONTS;
  374.  
  375.             CheckRadioButton(hDlg, ID_PRINTERFONTS, ID_BOTH, i);
  376.  
  377.             //Loop through dwFlags and check boxes for whatever is set.
  378.             for (i=0; i<7; i++)
  379.                 {
  380.                 if (dwFlags & mpIDFlag[i])
  381.                     CheckDlgButton(hDlg, i+3, TRUE);
  382.                 }
  383.             return TRUE;
  384.  
  385.  
  386.         case WM_COMMAND:
  387.             switch (LOWORD(wParam))
  388.                 {
  389.                 case IDOK:
  390.                     dwFlags=0;
  391.  
  392.                     //Get the required flags.
  393.                     if (IsDlgButtonChecked(hDlg, ID_BOTH))
  394.                         dwFlags=CF_BOTH;
  395.                     else
  396.                         {
  397.                         if (IsDlgButtonChecked(hDlg, ID_PRINTERFONTS))
  398.                             dwFlags=CF_PRINTERFONTS;
  399.                         else
  400.                             dwFlags=CF_SCREENFONTS;
  401.                         }
  402.  
  403.  
  404.                     //Loop through the options and twiddle dwFlags as necessary.
  405.                     for (i=0; i<7; i++)
  406.                         {
  407.                         if (IsDlgButtonChecked(hDlg, i+3))
  408.                             dwFlags|=mpIDFlag[i];
  409.                         }
  410.  
  411.                     EndDialog(hDlg, TRUE);
  412.                     break;
  413.                 }
  414.             break;
  415.  
  416.         default:
  417.             break;
  418.         }
  419.  
  420.     return FALSE;
  421.     }
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428. /*
  429.  * PopupWndProc
  430.  *
  431.  * Purpose:
  432.  *  Window class procedure for a popup window used as the owner of
  433.  *  dialogs that we want to behave as modeless.  Does nothing since
  434.  *  we never show it.
  435.  *
  436.  * Parameters:
  437.  *  Standard.
  438.  *
  439.  * Return Value:
  440.  *  Standard.
  441.  */
  442.  
  443. LONG FAR PASCAL PopupWndProc(HWND hWnd, UINT iMsg, UINT wParam, LONG lParam)
  444.     {
  445.     return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  446.     }
  447.  
  448.  
  449.  
  450. /*
  451.  * AboutProc
  452.  *
  453.  * Purpose:
  454.  *  Dialog procedure for the ubiquitous About box.
  455.  *
  456.  * Parameters:
  457.  *  The standard.
  458.  *
  459.  * Return Value:
  460.  *  The value to be returned through the DialogBox call that
  461.  *  created the dialog.
  462.  *
  463.  */
  464.  
  465. BOOL FAR PASCAL AboutProc(HWND hDlg, UINT iMsg, UINT wParam, LONG lParam)
  466.     {
  467.     switch (iMsg)
  468.         {
  469.         case WM_INITDIALOG:
  470.             return TRUE;
  471.  
  472.         case WM_COMMAND:
  473.             /*
  474.              * Only the OK button can send WM_COMMAND, so we ignore what
  475.              * the id in LOWORD(wParam) really is.
  476.              */
  477.             EndDialog(hDlg, TRUE);
  478.             break;
  479.         }
  480.  
  481.     return FALSE;
  482.     }
  483.